package com.example.HaiDLaobackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.HaiDLaobackend.common.ApiResponse;
import com.example.HaiDLaobackend.entity.UserFavoriteEntity;
import com.example.HaiDLaobackend.service.UserFavoriteService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/favorites")
public class FavoriteController {

    private final UserFavoriteService userFavoriteService;

    public FavoriteController(UserFavoriteService userFavoriteService) {
        this.userFavoriteService = userFavoriteService;
    }

    @GetMapping
    public ApiResponse<List<UserFavoriteEntity>> list(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        List<UserFavoriteEntity> list = userFavoriteService.list(new LambdaQueryWrapper<UserFavoriteEntity>()
                .eq(UserFavoriteEntity::getUserId, currentUserId)
                .orderByDesc(UserFavoriteEntity::getId));
        return ApiResponse.ok(list);
    }

    @GetMapping("/check")
    public ApiResponse<Boolean> check(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                      @RequestParam Long productId) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        long c = userFavoriteService.count(new LambdaQueryWrapper<UserFavoriteEntity>()
                .eq(UserFavoriteEntity::getUserId, currentUserId)
                .eq(UserFavoriteEntity::getProductId, productId));
        return ApiResponse.ok(c > 0);
    }

    @PostMapping("/{productId}")
    public ApiResponse<Boolean> add(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                    @PathVariable Long productId) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        long c = userFavoriteService.count(new LambdaQueryWrapper<UserFavoriteEntity>()
                .eq(UserFavoriteEntity::getUserId, currentUserId)
                .eq(UserFavoriteEntity::getProductId, productId));
        if (c > 0) return ApiResponse.ok(true);
        UserFavoriteEntity entity = new UserFavoriteEntity();
        entity.setUserId(currentUserId);
        entity.setProductId(productId);
        userFavoriteService.save(entity);
        return ApiResponse.ok(true);
    }

    @DeleteMapping("/{productId}")
    public ApiResponse<Boolean> remove(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                       @PathVariable Long productId) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        boolean ok = userFavoriteService.remove(new LambdaQueryWrapper<UserFavoriteEntity>()
                .eq(UserFavoriteEntity::getUserId, currentUserId)
                .eq(UserFavoriteEntity::getProductId, productId));
        return ApiResponse.ok(ok);
    }
}


